"""
对数据文件进行可视化描述
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

pd.set_option('display.float_format', lambda x: '%.3f' % x)  # 不使用科学计数法
plt.rcParams['font.sans-serif'] = ['SimHei']  # 在统计图上显示中文
# plt.style.use({'figure.figsize':(24, 9)})  #设置画布大小

# 读取目录下所有的csv文件,读取文件时将文件第一行作为列名

df1 = pd.read_csv('consistent_hash_result_leaf_switch4_server18.csv')
df2 = pd.read_csv('common_hash_result_leaf_switch4_server18.csv')
df3 = pd.read_csv('consistent_hash_result_leaf_switch4_server24.csv')
df4 = pd.read_csv('common_hash_result_leaf_switch4_server24.csv')
df5 = pd.read_csv('consistent_hash_result_leaf_switch4_server30.csv')
df6 = pd.read_csv('common_hash_result_leaf_switch4_server30.csv')
df7 = pd.read_csv('consistent_hash_result_leaf_switch4_server36.csv')
df8 = pd.read_csv('common_hash_result_leaf_switch4_server36.csv')

# 首先绘制网络拓扑相同时，一致性Hash算法命中随副本值的变化曲线，在一副图像中绘制4条曲线，并且在曲线中标出点，同时带上图例
# plt.figure(figsize=(12, 6))
# plt.plot(df1['replica_number_in_leaf_switch'],df1['hit_accuracy'],label='consistent hash with leaf_switch_number=4 and server_number=18',marker='o')
# plt.plot(df3['replica_number_in_leaf_switch'],df3['hit_accuracy'],label='consistent hash with leaf_switch_number=4 and server_number=24',marker='*')
# plt.plot(df5['replica_number_in_leaf_switch'],df5['hit_accuracy'],label='consistent hash with leaf_switch_number=4 and server_number=30',marker='o')
# plt.plot(df7['replica_number_in_leaf_switch'],df7['hit_accuracy'],label='consistent hash with leaf_switch_number=4 and server_number=36',marker='*')
# plt.title('一致性Hash算法命中率随副本数量变化情况')
# plt.xlabel('replica_number_in_leaf_switch')
# plt.ylabel('hit_accuracy')
# plt.legend()
# plt.savefig('一致性Hash算法命中率随副本数量变化情况.png')

# 接下来绘制网络拓扑相同时，普通Hash算法命中随副本值的变化曲线，在一副图像中绘制4条曲线，并且在曲线中标出点，同时带上图例
# plt.figure(figsize=(12, 6))
# plt.plot(df2['replica_number_in_leaf_switch'],df2['hit_accuracy'],label='common hash with leaf_switch_number=4 and server_number=18',marker='o')
# plt.plot(df4['replica_number_in_leaf_switch'],df4['hit_accuracy'],label='common hash with leaf_switch_number=4 and server_number=24',marker='*')
# plt.plot(df6['replica_number_in_leaf_switch'],df6['hit_accuracy'],label='common hash with leaf_switch_number=4 and server_number=30',marker='o')
# plt.plot(df8['replica_number_in_leaf_switch'],df8['hit_accuracy'],label='common hash with leaf_switch_number=4 and server_number=36',marker='*')
# plt.title('普通Hash算法命中率随副本数量变化情况')
# plt.xlabel('replica_number_in_leaf_switch')
# plt.ylabel('hit_accuracy')
# plt.legend()
# plt.savefig('普通Hash算法命中率随副本数量变化情况.png')
# plt.clf()
#
# # 接下来绘制网络结构相同时，普通Hash算法和一致性Hash算法命中率随副本值的变化曲线，以df3和df4为例
# plt.figure(figsize=(12, 6))
# plt.plot(df3['replica_number_in_leaf_switch'],df3['hit_accuracy'],label='consistent hash with leaf_switch_number=4 and server_number=24',marker='o')
# plt.plot(df4['replica_number_in_leaf_switch'],df4['hit_accuracy'],label='common hash with leaf_switch_number=4 and server_number=24',marker='*')
# plt.title('普通Hash算法和一致性Hash算法命中率随副本数量变化情况')
# plt.xlabel('replica_number_in_leaf_switch')
# plt.ylabel('hit_accuracy')
# plt.legend()
# plt.savefig('普通Hash算法和一致性Hash算法命中率随副本数量变化情况.png')
# plt.clf()

# 接下来绘制服务器数据分布情况，计算服务器数据分布的标准差，绘制标准差随副本值的变化曲线，在一副图像中绘制4条曲线
# plt.figure(figsize=(18, 8))
# for i in range(18, 37, 6):
#     df = pd.read_csv('consistent_hash_result_leaf_switch4_server%d.csv' % i)
#     # 计算标准差
#     stds = []
#     for j in range(len(df)):
#         caches = []
#         for m in range(i):
#             if 'server:%s_cache_size' % str(m) not in df.columns:
#                 continue
#             caches.append(df.loc[df.index[j], 'server:%s_cache_size' % str(m)])
#         caches = np.array(caches)
#         std = np.std(caches)
#         stds.append(std)
#     df['server_cache_size_std'] = stds
#     df2 = pd.read_csv('common_hash_result_leaf_switch4_server%d.csv' % i)
#     # 计算标准差
#     stds = []
#     for j in range(len(df)):
#         caches = []
#         for m in range(i):
#             if 'server:%s_cache_size' % str(m) not in df.columns:
#                 continue
#             caches.append(df2.loc[df2.index[j], 'server:%s_cache_size' % str(m)])
#         caches = np.array(caches)
#         std = np.std(caches)
#         stds.append(std)
#     df2['server_cache_size_std2'] = stds
#     plt.plot(df['replica_number_in_leaf_switch'], df['server_cache_size_std'],
#              label='consistent hash with leaf_switch_number=4 and server_number=%d' % i, marker='o')
#     plt.plot(df2['replica_number_in_leaf_switch'], df2['server_cache_size_std2'],
#              label='common hash with leaf_switch_number=4 and server_number=%d' % i, marker='*')
# plt.title('服务器数据分布标准差随副本数量变化情况')
# plt.xlabel('replica_number_in_leaf_switch')
# plt.ylabel('server_cache_size_std')
# plt.legend()
# plt.savefig('算法服务器数据分布标准差随副本数量变化情况.png')
# plt.clf()

# 最后一幅图绘制柱状图，绘制不同网络拓扑下服务器数据迁移量总数随网络拓扑变换的情况
plt.figure(figsize=(16,8))
net_topology = ['leaf_switch4\nserver18', 'leaf_switch4\nserver24', 'leaf_switch4\nserver30', 'leaf_switch4\nserver36']
# 对于一致性Hash算法取不同副本情况下的平均值，对于普通Hash算法直接计算即可
total_data_shift_number_in_consistent_hash = []
total_data_shift_number_in_common_hash = []
for i in range(18, 37, 6):
    df = pd.read_csv('consistent_hash_result_leaf_switch4_server%d.csv' % i)
    df2 = pd.read_csv('common_hash_result_leaf_switch4_server%d.csv' % i)
    total_data_shift_number_in_consistent_hash.append((df['request_number']-df['hit_number']).mean())
    total_data_shift_number_in_common_hash.append((df2['request_number']-df2['hit_number']).mean())
x = np.arange(len(net_topology)) # x轴刻度标签位置
width = 0.25  # 柱子的宽度
# 计算每个柱子在x轴上的位置，保证x轴刻度标签居中
# x - width/2，x + width/2即每组数据在x轴上的位置
plt.bar(x - width/2, total_data_shift_number_in_consistent_hash, width, label='total_data_shift_number_in_consistent_hash')
plt.bar(x + width/2, total_data_shift_number_in_common_hash, width, label='total_data_shift_number_in_common_hash')
plt.xticks(x, net_topology)
plt.title('不同网络拓扑下服务器数据迁移量总数随网络拓扑变换的情况')
plt.xlabel('net_topology')
plt.ylabel('times')
plt.legend()
plt.savefig('不同网络拓扑下服务器数据迁移量总数随网络拓扑变换的情况.png')

# # 首先从df1中提取出需要的数据,按照列提取
# print(df1.columns)
# df1_data=df1[['replica_number_in_leaf_switch','hit_accuracy']]
# # 将数据转换为DataFrame类型
# df1_data=pd.DataFrame(df1_data)
# # 将数据按照replica_number_in_leaf_switch进行排序
# df1_data.sort_values(by='replica_number_in_leaf_switch',inplace=True)
# # 绘制图形
# df1_data.plot(x='replica_number_in_leaf_switch',y='hit_accuracy',title='consistent hash with leaf_switch_number=4 and server_number=18')
# plt.show()
